﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.Model;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// 前台栏目(菜单)解析类
    /// </summary>
    class MenuModel
    {
        private const int CN_HOME_ID = ConConstants.HOME_ID;
        private int cid; //当前ID
        private static IList columnlist; //所有栏目
        private static Hashtable columnmap; //栏目map


        public MenuModel()
        {
            cid = CN_HOME_ID;
            init();
        }
        public MenuModel(int cid)
        {
            this.cid = cid;
            init();
        }
        //初始栏目数据
        private void init()
        {
            bool isupdate = ColumnBll.IsUpdate();
            if (columnlist == null || isupdate)
            {
                columnlist = ColumnBll.GetColumnList();
            }
            if (columnmap == null || isupdate)
            {
                columnmap = new Hashtable();
                for (int i = 0; i < columnlist.Count; i++)
                {
                    ColumnModel cm = (ColumnModel)columnlist[i];
                    columnmap.Add(cm.Classid, cm);
                }
            }
        }
        /// <summary>
        /// 返回当前栏目信息
        /// </summary>
        /// <returns></returns>
        public ColumnModel GetColumn()
        {
            return GetColumn(cid);
        }
        /// <summary>
        /// 返回栏目信息
        /// </summary>
        /// <param name="_cid"></param>
        /// <returns></returns>
        public ColumnModel GetColumn(int _cid)
        {
            return (ColumnModel)columnmap[_cid];
        }
        /// <summary>
        /// 获取子栏目
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IList GetSonColumn(int pid)
        {
            IList datalist = new ArrayList();
            for (int i = 0; i < columnlist.Count; i++)
            {
                ColumnModel cm = (ColumnModel)columnlist[i];
                if (cm.Parentid == pid)
                {
                    datalist.Add(cm);
                }
            }
            return datalist;
        }
        /// <summary>
        /// 获取层级所有子栏目
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IList GetLayeridColumn(int pid)
        {
            IList datalist = new ArrayList();
            ColumnModel pcm = GetColumn(pid);
            string layerid = pcm.Layerid;
            for (int i = 0; i < columnlist.Count; i++)
            {
                ColumnModel cm = (ColumnModel)columnlist[i];
                if (cm.Classid != pid && cm.Layerid.IndexOf(layerid) > -1)
                {
                    datalist.Add(cm);
                }
            }
            return datalist;
        }
    }
}
